package edu.whut.mall.admin.controller;


import edu.whut.mall.admin.domain.UserParams;
import edu.whut.mall.admin.service.ISysResourcesService;
import edu.whut.mall.admin.service.ISysUserRoleService;
import edu.whut.mall.admin.service.ISysUserService;
import edu.whut.mall.admin.shiro.util.ShiroUtil;
import edu.whut.mall.admin.vo.ResourceVo;
import edu.whut.mall.admin.vo.UserVo;
import edu.whut.mall.common.api.CommonPage;
import edu.whut.mall.common.api.CommonResult;
import edu.whut.mall.common.util.IpUtil;
import edu.whut.mall.model.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @program: d9lab-mall
 * @description: 用户控制器
 * @author: Wayne
 * @create: 2020-04-17 17:29
 **/
@Slf4j
@Api(value="用户管理器",tags = {"用户管理器"})
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class RestUserController {

    private final ISysUserService sysUserService;

    private final ISysUserRoleService sysUserRoleService;

    private final ISysResourcesService sysResourcesService;

    @ApiOperation("增加系统管理员用户")
    @PostMapping("/addOneUser")
    public CommonResult addOneUser(HttpServletRequest httpServletRequest,
                                @Valid @RequestBody UserParams userParams){
        SysUser result = sysUserService.insertOneUser(IpUtil.getIpAddr(httpServletRequest),userParams);
        if (result != null) {
            return CommonResult.success("添加用户成功");
        }
        return CommonResult.failed("添加用户失败");
    }


    @ApiOperation("为用户指定角色")
    @PostMapping("/resignRoleToUser")
    public CommonResult resignRoleToUser(@NonNull @RequestParam Long userId,
                                         @NotBlank @RequestParam String roleIdString){
        int result = sysUserRoleService.assignRole2User(userId,roleIdString);
        if (result >= 0) {
            return CommonResult.success("为用户分配角色成功");
        }
        return CommonResult.failed("为用户分配角色失败");
    }



    @ApiOperation(value = "获取用户的资源列表")
    @GetMapping("/getResourcesByUser")
    public CommonResult listResourcesByUser(){
        Long userId = ShiroUtil.getUserId();
        List<ResourceVo> resourceVos = sysResourcesService.listResourceVosByUserId(userId);
        if (CollectionUtils.isNotEmpty(resourceVos)) {
            return CommonResult.success(resourceVos,"获取树形资源成功");
        }
        return CommonResult.failed("没有此用户的资源");
    }

    @ApiOperation("获取所有管理员列表")
    @GetMapping("/getAdminList")
    public CommonResult getAdminList( @RequestParam(value="page",defaultValue = "1") Integer page,
                                      @RequestParam(value="size",defaultValue ="10") Integer size){
       CommonPage<UserVo> userVos = sysUserService.listAdmins(page, size);
        if (userVos != null) {
            return CommonResult.success(userVos,"获取管理员列表成功");
        }
        return CommonResult.failed("获取管理员列表失败");
    }

    @ApiOperation(value = "删除用户")
    @PostMapping("/deleteOneUser")
    public CommonResult deleteOneUser(@RequestParam Long userId){
        int result = sysUserService.deleteOneAdminUser(userId);
        if (result >= 0) {
            return CommonResult.success("删除角色成功");
        }
        return CommonResult.failed("删除角色失败");
    }

    @ApiOperation(value = "更行用户")
    @PostMapping("/updateUser")
    public CommonResult updateUser(@RequestBody UserParams userParams) {
        int result = sysUserService.updateUser(userParams);
        if (result >= 0) {
            return CommonResult.success("更新管理员信息成功");
        }
        return CommonResult.failed("更新管理员信息失败");
    }
}
